package com.hly.musicbackground.model.auth.service.impl;

import com.hly.musicbackground.Enum.UserEnum;
import com.hly.musicbackground.Enum.UserLogEnum;
import com.hly.musicbackground.Enum.UserRootEnum;
import com.hly.musicbackground.Utils.*;
import com.hly.musicbackground.domain.User;
import com.hly.musicbackground.exception.BusinessException;
import com.hly.musicbackground.model.auth.params.LoginParams;
import com.hly.musicbackground.model.auth.params.RegisterParams;
import com.hly.musicbackground.model.auth.service.LoginService;
import com.hly.musicbackground.model.auth.service.VerificationMailService;
import com.hly.musicbackground.model.user.service.UserLogService;
import com.hly.musicbackground.model.user.service.UserService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class LoginServiceImpl implements LoginService {

    private final UserService userService;
    private final UserLogService userLogService;
    private final TokenUtils tokenUtils;
    private final VerificationMailService verificationMailService;

    private final MD5Utils md5Utils;
    private final PathUtils pathUtils;
    private final Base64Utils base64Utils;

    @Override
    public String login(LoginParams loginParams) {
        User user = userService.getUserInfo(loginParams.getMail(), loginParams.getPassword());
        if (user == null){
            throw new BusinessException(Code.BUSINESS_ERR, "邮箱或密码错误");
        }
        userLogService.addLog(user.getUserId(), 0, UserLogEnum.LOGIN, "");
        String token = tokenUtils.getToken(user.getUserId(), user.getRoot());
        return token;
    }

    @Override
    public String loginMail(LoginParams loginParams) {
        verificationMailService.isVerificationMail(loginParams.getMail(),loginParams.getSms());
        User user = userService.getUserInfo(loginParams.getMail());
        if (user == null){
            throw new BusinessException(Code.BUSINESS_ERR, "邮箱错误");
        }
        String token = tokenUtils.getToken(user.getUserId(), user.getRoot());
        return token;
    }

    @Override
    public String register(RegisterParams params) {

        // 检查验证码
        verificationMailService.isVerificationMail(params.getMail(), params.getSms());

        // 检查用户名称是否被占用
        User one = userService.lambdaQuery()
                .eq(User::getUserName, params.getUserName())
                .one();
        if (one != null){
            Code.BUSINESS_ERR("用户名已被占用");
        }

        // 检查邮箱是否被占用
        if (userService.lambdaQuery()
                .eq(User::getMail, params.getMail())
                .one() != null){
            Code.BUSINESS_ERR("邮箱已被占用");
        }

        User user = new User();
        user.setMail(params.getMail());
        user.setRoot(UserRootEnum.USER.getValue());
        user.setPassword(md5Utils.encrypt(params.getPassword()));
        user.setUuid(UUID.randomUUID().toString());
        user.setUserName(params.getUserName());
        user.setUserImage(params.getUserImage());

        // 创建用户文件夹
        String replace = "\\" + params.getMail().replace("@", "");
        FileUtils.file(pathUtils.path + pathUtils.imagesPath + replace);

        if (StringUtils.isNotEmpty(params.getBase64())){
            String s = base64Utils.GenerateImage(params.getBase64(),
                    replace + UserEnum.HEAD_IMAGE.getValue());
            user.setUserImage(s);
        }

        userService.save(user);
        userLogService.addLog(user.getUserId(), 0, UserLogEnum.REGISTER, "");
        return tokenUtils.getToken(user.getUserId(), user.getRoot());
    }
}
